package com.zjy.common.constant;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangjianyu
 * email: 578547270@qq.com
 * create_time: 2024-10-18 09:37
 * description:
 */
@Getter
public enum StatusEnums {

    PUBLIC_STATUS_NORMAL(BusinessConstant.PUBLIC_STATUS, 1, "正常"),
    PUBLIC_STATUS_STOP(BusinessConstant.PUBLIC_STATUS, 0, "停用"),

    // 菜单类型
    MENU_TYPE_CATALOGUE(BusinessConstant.MENU_TYPE, "M", "目录"),
    MENU_TYPE_MENU(BusinessConstant.MENU_TYPE, "C", "菜单"),
    MENU_TYPE_BUTTON(BusinessConstant.MENU_TYPE, "F", "按钮"),

    // 用户登录类型
    LOGIN_USER_TYPE_ADMIN(BusinessConstant.LOGIN_USER_TYPE_KEY, "1", "后台登录用户"),
    ;

    /**
     * 业务代码
     */
    private String businessCode;

    /**
     * 状态值
     */
    private Integer status;

    /**
     * 状态值(-字符串)
     */
    private String statusStr;

    /**
     * 状态描述
     */
    private String statusMes;

    StatusEnums(String businessCode, Integer status, String statusMes) {
        this.businessCode = businessCode;
        this.status = status;
        this.statusMes = statusMes;
    }

    StatusEnums(String businessCode, String status, String statusMes) {
        this.businessCode = businessCode;
        this.statusStr = status;
        this.statusMes = statusMes;
    }

    private final static Map<String, Map<Integer, StatusEnums>> MAP_DATA = new HashMap<>();

    static {
        for (StatusEnums value : StatusEnums.values()) {
            String code = value.getBusinessCode();
            Map<Integer, StatusEnums> map = MAP_DATA.getOrDefault(code, new HashMap<>());
            map.put(value.getStatus(), value);
            MAP_DATA.put(code, map);
        }
    }

    /**
     * 根据业务代码和状态码获取对应的状态信息
     * 此方法主要用于将给定的业务代码和状态码映射到相应的状态信息字符串
     * 如果没有找到匹配的状态枚举，则返回null
     *
     * @param businessCode 业务代码，表示特定的业务场景
     * @param status       状态码，表示业务场景中的具体状态
     * @return 如果找到匹配的状态枚举，则返回对应的状态信息字符串；否则返回null
     */
    public static String of(String businessCode, Integer status) {
        StatusEnums statusEnums = ofEnum(businessCode, status);
        if (statusEnums != null) {
            return statusEnums.getStatusMes();
        }
        return null;
    }

    /**
     * 根据业务代码和状态码获取对应的状态枚举
     *
     * @param businessCode 业务代码，用于区分不同的业务场景
     * @param status       状态码，用于在特定业务场景下识别特定的状态
     * @return 对应的状态枚举，如果找不到对应的枚举则返回null
     */
    public static StatusEnums ofEnum(String businessCode, Integer status) {
        Map<Integer, StatusEnums> dataMap = MAP_DATA.get(businessCode);
        if (dataMap != null) {
            return dataMap.get(status);
        }
        return null;
    }
}
